import FrontApi from '@/api/front'
import {defineStore} from "pinia";


const useCatalogueStore = defineStore("catalogue", {
    state: () => ({
        catalogueList: [],
        catalogueList2: [],
        catalogueList3: [],
        catalogueList4: [],
    }),
    actions: {
        getCatalogue(type) {
            return new Promise((resolve, reject) => {
                let catalogue
                if (type === '1') {
                    catalogue = this.catalogueList
                } else if (type === '2') {
                    catalogue = this.catalogueList2
                } else if (type === '3') {
                    catalogue = this.catalogueList3
                } else {
                    catalogue = this.catalogueList4
                }
                if (catalogue.length) {
                    resolve(catalogue)
                } else {
                    FrontApi.getCatalogue(type).then(res => {
                        let catalogueList = res.data || []
                        resolve(catalogueList)
                        if (type === '1') {
                            this.catalogueList = catalogueList
                        } else if (type === '2') {
                            this.catalogueList2 = catalogueList
                        } else if (type === '3') {
                            this.catalogueList3 = catalogueList
                        } else {
                            this.catalogueList4 = catalogueList
                        }
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        },
        getSolutionCatalogue() {
            return new Promise((resolve, reject) => {
                if (this.catalogueList2.length) {
                    resolve(this.catalogueList2)
                } else {
                    FrontApi.getCatalogue(3).then(res => {
                        let catalogueList = res.data || []
                        resolve(catalogueList)
                        this.catalogueList2 = catalogueList
                    }).catch(error => {
                        reject(error)
                    })
                }
            })
        },
        /**
         * 更新目录列表
         **/
        updateCatalogue(type) {
            return new Promise((resolve, reject) => {
                FrontApi.getCatalogue(type).then(res => {
                    let catalogueList = res.data || []
                    if (Number(type) === 1) {
                        this.catalogueList = catalogueList
                    } else if (Number(type) === 2) {
                        this.catalogueList2 = catalogueList
                    } else if (Number(type) === 3) {
                        this.catalogueList3 = catalogueList
                    } else {
                        this.catalogueList4 = catalogueList
                    }
                    resolve(catalogueList)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        /**
         * 更新解决方案目录列表
         **/
        updateSolutionCatalogue() {
            return new Promise((resolve, reject) => {
                FrontApi.getCatalogue(3).then(res => {
                    let catalogueList = res.data || []
                    resolve(catalogueList)
                    this.catalogueList2 = catalogueList
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }

})


export default useCatalogueStore